﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;

namespace SharpSoft.Web
{
    public class ImageHelper
    {
        /// <summary>
        /// 重新定义图片的尺寸
        /// </summary>
        /// <param name="srcImage">原图像</param>
        /// <param name="iWidth">要改到的宽度</param>
        /// <param name="iHeight">高改到的高度</param>
        /// <param name="resetbg">是否重置图片的背景色（用于png图转为其他不带透明通道的图片类型时）</param>
        /// <param name="keepScale">是否保持图片宽高比</param>
        /// <returns>返回重新缩放后的新图片</returns>
        public static Bitmap SizeImage(Image srcImage, int iWidth, int iHeight, Color? resetbg = null, bool keepScale = true)
        {
            try
            {
                int nw, nh;
                if (keepScale)
                {
                    var sw = iWidth / (float)srcImage.Width;
                    var sh = iHeight / (float)srcImage.Height;
                    var scale = Math.Min(sw, sh);
                    nw = (int)(srcImage.Width * scale);
                    nh = (int)(srcImage.Height * scale);
                }
                else
                {
                    nw = iWidth;
                    nh = iHeight;

                }
                if (nw <= 0)
                {
                    nw = 1;
                }
                if (nh <= 0)
                {
                    nh = 1;
                }
                // 要保存到的图片 
                Bitmap b = new Bitmap(nw, nh);
                Graphics g = Graphics.FromImage(b);
                // 插值算法的质量 
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                if (resetbg.HasValue)
                {
                    g.DrawRectangle(new Pen(resetbg.Value), new Rectangle(0, 0, nw, nh));
                }
                g.DrawImage(srcImage, new Rectangle(0, 0, nw, nh), new Rectangle(0, 0, srcImage.Width, srcImage.Height), GraphicsUnit.Pixel);
                g.Dispose();
                return b;
            }
            catch (Exception ex)
            {
                throw new Exception("重置图片尺寸失败。");
            }
        }

        public static Stream SizeImage(Stream srcImage, int iWidth, int iHeight, Color? resetbg = null, bool keepScale = true)
        {
            var img = Bitmap.FromStream(srcImage);
            var newbmp = SizeImage(img, iWidth, iHeight, resetbg, keepScale);
            var ms = new MemoryStream();
            newbmp.Save(ms, img.RawFormat);
            ms.Seek(0, SeekOrigin.Begin);
            return ms;
        }
        /// <summary>
        /// 创建指定大小的透明图片
        /// </summary>
        /// <param name="iWidth"></param>
        /// <param name="iHeight"></param>
        /// <returns></returns>
        public static Stream TransparencyImage(int iWidth, int iHeight)
        {
            var bmp = new Bitmap(iWidth, iHeight) { };
            var ms = new MemoryStream();

            bmp.Save(ms, ImageFormat.Png);
            ms.Seek(0, SeekOrigin.Begin);
            return ms;
        }
    }
}
